/**
 * Created by hdr on 17/8/17.
 */


const WeightReport = require('./Weight')
const HeightReport = require('./Height')
const BMIReport = require('./BMI')
const BodyfatReport = require('./Bodyfat')
const WaterReport = require('./Water')
const MuscleReport = require('./Muscle')
const MuscleMassReport = require('./MuscleMass')
const BoneReport = require('./Bone')
const LbmReport = require('./Lbm')
const ProteinReport = require('./Protein')
const SubfatReport = require('./Subfat')
const VisfatReport = require('./Visfat')
const BodyAgeReport = require('./BodyAge')
const BodyShapeReport = require('./BodyShape')
const BmrReport = require('./Bmr')
const HeartRateReport = require('./HeartRate')
const HeartIndexReport = require('./HeartIndex')


class Report {

  reportItemList = []

  constructor(measure, params = {}) {
    this.params = params
    this.measure = measure
    this._initReportItem()
  }

  get score() {
    return this.measure.score || 0
  }

  get measureTime() {
    return this.measure.time
  }

  isValid() {
    return this.measure.bodfat > 0;
  }

  _initReportItem() {
    const arr = [];
    const reports = [
      WeightReport,
      HeightReport,
      HeartRateReport,
      HeartIndexReport,
      BMIReport,
      BodyfatReport,
      WaterReport,
      MuscleReport,
      MuscleMassReport,
      BoneReport,
      LbmReport,
      ProteinReport,
      SubfatReport,
      VisfatReport,
      BodyAgeReport,
      BodyShapeReport,
      BmrReport,
    ]

    reports.forEach(report => {
      if (report.isEnable(this.measure)) {
        const item = report.build(this.measure)
        if (!item) return 
          if (item.fixLevel > 0 && item.value.toFixed) {
            item.value = parseFloat(item.value.toFixed(item.fixLevel))
            if (item.offset != 0) {
              item.offset = parseFloat(parseFloat(item.offset).toFixed(item.fixLevel))
            }
            if (item.boundaries && item.boundaries.length > 0)
              item.boundaries = item.boundaries.map(value => parseFloat(value.toFixed(item.fixLevel)))
          }
          if (item.showBar) {
            if (item.min > item.value) 
              item.min = item.value
            
            if (item.max < item.value) 
              item.max = item.value
            
          }
          arr.push(item)
      }
    })

    this.reportItemList = arr
  }
}

module.exports = Report